package com.geovis.emergency.spd.biz.command.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.command.mapper.TfMapper;
import com.geovis.emergency.spd.biz.command.mapper.TfljMapper;
import com.geovis.emergency.spd.biz.command.mapper.TfybljMapper;
import com.geovis.emergency.spd.biz.command.service.TfService;
import com.geovis.emergency.spd.entity.command.entity.OdsZgtfwTyphoon;
import com.geovis.emergency.spd.entity.command.entity.Tf;
import com.geovis.emergency.spd.entity.command.entity.Tflj;
import com.geovis.emergency.spd.entity.command.entity.Tfyblj;
import com.geovis.emergency.spd.entity.command.enums.TfTypeEnum;
import com.geovis.emergency.spd.entity.command.pojo.vo.OdsTyphoonPointVO;
import com.geovis.emergency.spd.entity.command.pojo.vo.OdsTyphoonVO;
import com.geovis.emergency.spd.entity.command.pojo.vo.TfForecastVo;
import com.geovis.emergency.spd.entity.command.pojo.vo.TfljDetailVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
//@DS("ods")
public class TfServiceImpl extends ServiceImpl<TfMapper, Tf> implements TfService {
    private final TfljMapper tfljMapper;
    private final TfybljMapper tfybljMapper;

    @Override
    public Tf getByTyphoonNo(String typhoonNo) {
        return baseMapper.selectOne(
                Wrappers.lambdaQuery(Tf.class)
                        .eq(Tf::getTyphoonId, typhoonNo)
        );
    }

    @Override
    public List<Tf> getList() {
        return baseMapper.selectList(
                Wrappers.lambdaQuery(Tf.class).orderByDesc(Tf::getStartTime)
        );
    }
    @Override
    public String getTyphoonLatestPointId(String typhoonNo)
    {
        Wrapper<Tflj> wrapper = Wrappers.lambdaQuery(Tflj.class)
                .eq(Tflj::getTyphoonId, typhoonNo)
                .orderByDesc(Tflj::getTime)
                .last("limit 1");
        Tflj point=tfljMapper.selectOne(wrapper);
        if(point==null)
        {
            return null;
        }
        return point.getId();
    }

    @Override
    public OdsTyphoonVO getTyphoon(String typhoonNo) {
        Tf typhoon=getByTyphoonNo(typhoonNo);
        if (typhoon==null)
        {
            return null;
        }
        String year=typhoon.getYear();
        OdsTyphoonVO typhoonVO = BeanUtil.toBean(typhoon, OdsTyphoonVO.class);
        //设置基本信息
        typhoonVO.setName(typhoon.getCnName())
                .setEname(typhoon.getEnName())
                .setTyphoonNo(typhoon.getTyphoonId());
        typhoonVO.setBegin_time(LocalDateTime.parse(typhoon.getStartTime()+":00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        typhoonVO.setEnd_time(LocalDateTime.parse(typhoon.getStopTime()+":00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        Wrapper<Tflj> wrapper = Wrappers.lambdaQuery(Tflj.class)
                .eq(Tflj::getTyphoonId, typhoonNo)
                .orderByDesc(Tflj::getTime);
        List<Tflj> tfljs=tfljMapper.selectList(wrapper);
        List<OdsTyphoonPointVO> points=new ArrayList<>();
        for(int x=0;x<tfljs.size();x++){
            Tflj point=tfljs.get(x);
            OdsTyphoonPointVO pointVo = new OdsTyphoonPointVO();
            pointVo.setPointId(point.getId());
            LocalDateTime time=LocalDateTime.parse(year+"-"+point.getTime()+":00",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            pointVo.setTime(time);
            pointVo.setStrong(TfTypeEnum.getNameByCode(point.getType()));
            if(StrUtil.isNotBlank(point.getLat())) {
                pointVo.setLatitude(Double.valueOf(point.getLat()));
            }
            if(StrUtil.isNotBlank(point.getLon())) {
                pointVo.setLongitude(Double.valueOf(point.getLon()));
            }
            if(StrUtil.isNotBlank(point.getCenterWindPower())) {
                pointVo.setPower(new BigDecimal(point.getCenterWindPower()).intValue());
            }
            if(StrUtil.isNotBlank(point.getCenterWindSpeed())) {
                pointVo.setSpeed(new BigDecimal(point.getCenterWindSpeed()).intValue());
            }
            if(StrUtil.isNotBlank(point.getMoveSpeed())) {
                pointVo.setMove_speed(new BigDecimal(point.getMoveSpeed()).intValue());
            }
            if(StrUtil.isNotBlank(point.getCenterPressure())) {
                pointVo.setPressure(new BigDecimal(point.getCenterPressure()).intValue());
            }
            //处理圆形json7，圆形json10，圆形json12
            Map<String,String> radius7Map=new HashMap<>();
            radius7Map.put("sw",point.getCir7Sw());
            radius7Map.put("nw",point.getCir7Nw());
            radius7Map.put("ne",point.getCir7Ne());
            radius7Map.put("se",point.getCir7Se());
            pointVo.setRadius7_quad(JSONUtil.toJsonStr(radius7Map));

            Map<String,String> radius10Map=new HashMap<>();
            radius10Map.put("sw",point.getCir10Sw());
            radius10Map.put("nw",point.getCir10Nw());
            radius10Map.put("ne",point.getCir10Ne());
            radius10Map.put("se",point.getCir10Se());
            pointVo.setRadius10_quad(JSONUtil.toJsonStr(radius10Map));

            Map<String,String> radius12Map=new HashMap<>();
            radius12Map.put("sw",point.getCir12Sw());
            radius12Map.put("nw",point.getCir12Nw());
            radius12Map.put("ne",point.getCir12Ne());
            radius12Map.put("se",point.getCir12Se());
            pointVo.setRadius12_quad(JSONUtil.toJsonStr(radius12Map));
            //预报路径

            Wrapper<Tfyblj> ybljWrapper = Wrappers.lambdaQuery(Tfyblj.class)
                    .eq(Tfyblj::getTyphoonId, typhoonNo)
                    .eq(Tfyblj::getForecastTime,point.getTime())
                    .orderByAsc(Tfyblj::getTime);
            List<Tfyblj> tfybljs=tfybljMapper.selectList(ybljWrapper);
            if(!CollectionUtils.isEmpty(tfybljs))
            {
                Map<String,List<Tfyblj>> tfybljsMap=tfybljs.stream().collect(Collectors.groupingBy(Tfyblj::getForecastOffice));
                List<TfForecastVo> forecastVos=tfybljsMap.keySet().stream().map(set->{
                    TfForecastVo forecastVo=new TfForecastVo();
                    forecastVo.setSets(set);
                    List<Tfyblj> ybljs=tfybljsMap.get(set);
                    List<TfljDetailVo> detailPoints=ybljs.stream().map(yblj->{
                        TfljDetailVo ybVo= TfljDetailVo.fromTfyblj(yblj);
                        ybVo.setTime(year+"-"+yblj.getTime());
                        return ybVo;
                    }).collect(Collectors.toList());
                    forecastVo.setPoints(detailPoints);
                    return forecastVo;
                }).collect(Collectors.toList());
                pointVo.setForecast(JSONUtil.toJsonStr(forecastVos));
            }
            points.add(pointVo);
        }
        typhoonVO.setPoints(points);
        return typhoonVO;
    }
}
